/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.showcase.client.activity;

import com.cleangwt.bootstrap.client.style.Btn;
import com.cleangwt.bootstrap.client.ui.GroupFormPanel;
import com.cleangwt.bootstrap.client.ui.PaginationPager;
import com.cleangwt.json.client.SimpleObject;
import com.cleangwt.showcase.client.ProvidesSource;
import com.cleangwt.showcase.client.resource.i18n.UserManagerMessages;
import com.cleangwt.uibinder.client.RequiresActionRebind;
import com.cleangwt.uibinder.client.UiHandler;
import com.cleangwt.user.cellview.client.DataGrid;
import com.cleangwt.user.cellview.client.DateColumn;
import com.cleangwt.user.cellview.client.NumberColumn;
import com.cleangwt.user.cellview.client.TextColumn;
import com.cleangwt.user.client.activity.ForwardableActivity;
import com.cleangwt.user.client.ui.Button;
import com.cleangwt.user.client.ui.Lay;
import com.cleangwt.user.client.ui.ListBox;
import com.cleangwt.web.client.ProvidesCriteria;
import com.cleangwt.web.client.RemoteOptions;
import com.cleangwt.web.client.RemotePagingProvider;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.BrowserEvents;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.view.client.CellPreviewEvent;

/**
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public abstract class CwUserList extends ForwardableActivity implements ProvidesCriteria,
    ProvidesSource, RequiresActionRebind {

  UserManagerMessages c = GWT.create(UserManagerMessages.class);
  public Button btnNew = new Button(c.add());
  public Button btnSearch = new Button(c.search());
  public DataGrid dataGrid = new DataGrid();
  public ListBox lstUser = new ListBox(c.all());

  public RemotePagingProvider dataProvider = new RemotePagingProvider("user/list.do");

  public PaginationPager pager = new PaginationPager();

  public CwUserList() {
    GroupFormPanel form = new GroupFormPanel();

    dataGrid.addColumn(new TextColumn("name", true), c.name());
    dataGrid.addColumn(new NumberColumn("age", true), c.age());
    dataGrid.addColumn(new TextColumn("address", true), c.address());
    dataGrid.addColumn(new DateColumn("birth", true), c.birth());
    dataGrid.getColumnSortList().setLimit(3);

    dataProvider.addDataDisplay(dataGrid);
    dataProvider.setCriteriaProvider(this);

    pager.setDisplay(dataGrid);
    // layout
    form.build().add(c.name(), lstUser).div(3);
    form.add(Lay.pullRight(Lay.p(btnSearch, Btn.style(btnNew))));
    form.add(Lay.height(240, Lay.hStretch(dataGrid)));
    initWidget(form);

    // use ActionManager to separate event binding
    // UserListAction actionBinder = GWT.create(UserListAction.class);
    // actionBinder.bind(this);

    this.addEventHandlers();
  }

  @Override
  public SimpleObject getCriteria() {
    SimpleObject criteria = new SimpleObject();
    criteria.put("id", lstUser.getValue());
    return criteria;
  }

  @Override
  public void onActive(int relatedIndex) {
    // always reloads options
    RemoteOptions.get("option/user.do", lstUser);
  }

  @UiHandler("btnNew")
  void onBtnNewClick(ClickEvent event) {
    CwUserDetail userDetail = getActivity(1);
    userDetail.setRecord(null);
    forward(1);
  }

  @UiHandler("btnSearch")
  void onBtnSearchClick(ClickEvent event) {
    dataProvider.fetch();
  }

  @UiHandler("dataGrid")
  void onDataGridCellPreview(CellPreviewEvent<SimpleObject> e) {
    if (BrowserEvents.CLICK.equals(e.getNativeEvent().getType())) {
      CwUserDetail userDetail = getActivity(1);
      userDetail.setRecord(e.getValue());
      this.forward(1);
    }
  }

}
